//重写甘特图表格头部右键菜单-列配置
PlusProject.PlanStatusColumn = function(optons) {
  return mini.copyTo(
    {
      name: 'statusPlan',
      header: '计划状态',
      field: 'executedStatus'
    },
    optons
  );
};
PlusProject.OwnerColumn = function(optons) {
  return mini.copyTo(
    {
      name: 'ownerDescription',
      header: '负责人',
      field: 'ownerDescription',
      renderer: function(e) {
        var row = e.record,
          value = e.value;
        return value ? value + '(' + row.ownerName + ')' : '';
      }
    },
    optons
  );
};
PlusProject.ExecutorColumn = function(optons) {
  return mini.copyTo(
    {
      name: 'executorName',
      header: '执行人',
      field: 'executorName'
    },
    optons
  );
};
PlusProject.ConfigColumns = [
  {
    name: 'checkcolumn',
    text: '选择列',
    column: function(config) {
      return $.extend({type: 'checkcolumn'}, config);
    }
  },
  {name: 'ID', text: '行号', column: PlusProject.IDColumn},
  {name: 'Name', text: '名称', column: PlusProject.NameColumn},
  // {name: 'Manual', text: '任务模式', column: PlusProject.ManualColumn},
  // {name: 'TaskType', text: '任务类型', column: PlusProject.TaskTypeColumn},
  {name: 'PredecessorLink', text: '前置关系', column: PlusProject.PredecessorLinkColumn},
  {name: 'Duration', text: '工期', column: PlusProject.DurationColumn},
  // {name: 'Work', text: '工时', column: PlusProject.WorkColumn},
  {name: 'PercentComplete', text: '完工率', column: PlusProject.PercentCompleteColumn},
  // {name: 'Assignments', text: '资源分配', column: PlusProject.AssignmentsColumn},
  {name: 'Start', text: '计划开始时间', column: PlusProject.StartColumn},
  {name: 'Finish', text: '计划完成时间', column: PlusProject.FinishColumn},
  // {name: 'ActualStart', text: '实际开始时间', column: PlusProject.ActualStartColumn},
  // {name: 'ActualFinish', text: '实际完成时间', column: PlusProject.ActualFinishColumn},
  // {name: 'EarlyStart', text: '最早开始时间', column: PlusProject.EarlyStartColumn},
  // {name: 'EarlyFinish', text: '最早完成时间', column: PlusProject.EarlyFinishColumn},
  // {name: 'LateStart', text: '最晚开始时间', column: PlusProject.LateStartColumn},
  // {name: 'LateFinish', text: '最晚完成时间', column: PlusProject.LateFinishColumn},
  // {name: 'TotalSlack', text: '可宽延的总时间', column: PlusProject.TotalSlackColumn},
  // {name: 'Critical', text: '关键任务', column: PlusProject.CriticalColumn},
  // {name: 'ConstraintType', text: '限制类型', column: PlusProject.ConstraintTypeColumn},
  // {name: 'ConstraintDate', text: '限制日期', column: PlusProject.ConstraintDateColumn},
  // {name: 'Deadline', text: '最后期限', column: PlusProject.DeadlineColumn},
  // {name: 'WBS', text: 'WBS', column: PlusProject.WBSColumn},
  {name: 'Status', text: '状态', column: PlusProject.StatusColumn},
  {name: 'statusPlan', text: '计划状态', column: PlusProject.PlanStatusColumn},
  // {name: 'Department', text: '部门', column: PlusProject.DepartmentColumn},
  {name: 'ownerDescription', text: '负责人', column: PlusProject.OwnerColumn},
  {name: 'executorName', text: '执行人', column: PlusProject.ExecutorColumn}
];
PlusProject.ColumnConfigWindow = PlusProject.Window.extend({
  props: {
    allColumns: [],
    columns: [],
    title: '列配置',
    width: 550,
    height: 350,
    footer:
      '<div style="display:flex;">' +
      '<mini-button ref="reset" @click="onResetClick" >恢复默认</mini-button>' +
      '<div style="flex:1"></div>' +
      '<mini-button ref="ok" @click="onOkClick" width="60" style="margin-right:10px;">确定</mini-button> <mini-button ref="cancel" @click="onCancelClick" width="60">取消</mini-button>' +
      '</div>',
    content:
      '<div style="display:flex;height:100%;">' +
      '<div style="flex:1;">' +
      '<mini-panel height="100%" showHeader="false" showToolbar="true" bodyStyle="padding:0;">' +
      '<toolbar>' +
      '<div style="font-weight:bold;">可用的列：</div>' +
      '</toolbar>' +
      '<mini-listbox ref="allColumns" @itemdblclick="handleRight" style="height:100%;width:100%;" borderStyle="border:0">' +
      '</mini-listbox>' +
      '</mini-panel>' +
      '</div>' +
      '<div style="display:flex;flex-direction: column;justify-content:center;padding:4px;width:60px;">' +
      '<mini-button @click="handleRight" text=">" style="margin:2px 0;"></mini-button>' +
      '<mini-button @click="handleLeft" text="<" style="margin:2px 0;"></mini-button>' +
      '<mini-button @click="handleRightAll" text=">>" style="margin:2px 0;"></mini-button>' +
      '<mini-button @click="handleLeftAll" text="<<" style="margin:2px 0;"></mini-button>' +
      '</div>' +
      '<div style="flex:1">' +
      '<mini-panel height="100%" showHeader="false" showToolbar="true" bodyStyle="padding:0;">' +
      '<toolbar>' +
      '<div style="font-weight:bold;">已选的列：</div>' +
      '</toolbar>' +
      '<mini-listbox ref="currentColumns" @itemdblclick="handleLeft" style="height:100%;width:100%;" borderStyle="border:0">' +
      '</mini-listbox>' +
      '</mini-panel>' +
      '</div>' +
      '<div style="display:flex;flex-direction: column;justify-content:center;padding:4px;width:60px;">' +
      '<mini-button @click="handleUp" text="向上" style="margin:2px 0;"></mini-button>' +
      '<mini-button @click="handleDown" text="向下" style="margin:2px 0;"></mini-button>' +
      '</div>' +
      '</div>'
  },

  setData: function() {
    this.currentColumns = this.project.getConfigColumns();
    console.log(this.currentColumns);
    this.allColumns = this.createAllColumns(this.currentColumns);
    this.refresh();
  },

  refresh: function() {
    this.refs.allColumns.setData(this.allColumns);
    this.refs.currentColumns.setData(this.currentColumns);
  },

  createAllColumns: function(currentColumns) {
    var allColumns = PlusProject.ConfigColumns.clone();

    function hasCurrentColumn(name) {
      var item = PlusProject.getConfigColumn(name, currentColumns);
      return !!item;
    }

    for (var i = allColumns.length - 1; i >= 0; i--) {
      var column = allColumns[i];

      if (hasCurrentColumn(column.name)) {
        allColumns.removeAt(i);
      }
    }
    return allColumns;
  },

  handleRight: function() {
    var item = this.refs.allColumns.getSelected();
    var index = this.allColumns.indexOf(item);
    if (item) {
      this.allColumns.remove(item);
      this.currentColumns.push(item);
      this.refresh();

      item = this.allColumns[index];
      if (!item) item = this.allColumns[index - 1];
      this.refs.allColumns.setSelected(item);
    }
  },

  handleLeft: function() {
    var item = this.refs.currentColumns.getSelected();
    var index = this.currentColumns.indexOf(item);
    if (item) {
      this.currentColumns.remove(item);
      this.allColumns = this.createAllColumns(this.currentColumns);
      this.refresh();

      item = this.currentColumns[index];
      if (!item) item = this.currentColumns[index - 1];
      this.refs.currentColumns.setSelected(item);
    }
  },

  handleRightAll: function() {
    this.currentColumns = this.currentColumns.concat(this.allColumns);
    this.allColumns.length = 0;
    this.refresh();
  },

  handleLeftAll: function() {
    this.currentColumns = [];
    this.allColumns = PlusProject.ConfigColumns.clone();
    this.refresh();
  },

  handleUp: function() {
    var item = this.refs.currentColumns.getSelected();
    var index = this.currentColumns.indexOf(item);
    if (item && index > 0) {
      this.currentColumns.removeAt(index);
      this.currentColumns.insert(index - 1, item);
      this.refresh();
      this.refs.currentColumns.setSelected(item);
    }
  },

  handleDown: function() {
    var item = this.refs.currentColumns.getSelected();
    var index = this.currentColumns.indexOf(item);
    if (item) {
      this.currentColumns.insert(index + 2, item);
      this.currentColumns.removeAt(index);
      this.refresh();
      this.refs.currentColumns.setSelected(item);
    }
  },

  onOkClick: function(e) {
    var project = this.project;

    var columns = this.currentColumns;

    var action = new PlusProject.ChangeConfigColumnsAction(project, columns);
    project.executeAction(action);

    this.close('ok');
  },

  onResetClick: function() {
    this.currentColumns = this.project.defaultConfigColumns.clone();
    this.allColumns = this.createAllColumns(this.currentColumns);
    this.refresh();
    this.onOkClick();
  }
});
